/*
 * Copyright (c) 2025 EasyAdmin
 * All rights reserved.
 *
 * This file is part of the [EasAdmin] project.
 * Unauthorized copying of this file, via any medium, is strictly prohibited.
 * Proprietary and confidential.
 *
 * Author: EasyAdmin ( 1073602@qq.com )
 * Website: https://www.xhxiao.com
 */
package com.easy.app.controller.app;

import com.easy.framework.common.model.R;
import com.easy.framework.common.validate.Add;
import com.easy.framework.common.validate.Update;
import com.easy.framework.database.page.Pagination;
import com.easy.framework.poi.utils.ExcelUtils;
import com.easy.framework.web.base.BaseController;
import com.easy.module.log.annotation.Log;
import com.easy.module.log.enums.Operation;
import com.easy.module.system.entity.SysAppVersion;
import com.easy.module.system.query.SysAppVersionQuery;
import com.easy.module.system.service.ISysAppVersionService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 应用版本 控制器
 *
 * @author EasyAdmin ( 1073602@qq.com )
 * @date 2025-08-20
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("app/version")
public class SysAppVersionController extends BaseController
{

    final ISysAppVersionService sysAppVersionService;

    /**
    * 获取应用版本列表
    */
    @PreAuthorize("@perm.hasPermission('app:version$list')")
    @GetMapping("/page")
    public R<Pagination<SysAppVersion>> page(SysAppVersionQuery query) {
        return R.success(sysAppVersionService.page(query));
    }

    @Log(title = "应用版本管理", type = Operation.EXPORT)
    @PreAuthorize("@perm.hasPermission('app:version$export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysAppVersionQuery query) throws IOException {
        List<SysAppVersion> list = sysAppVersionService.list(query);
        ExcelUtils.exportExcel(response, list, "应用版本数据");
    }

    /**
    * 根据应用版本编号获取详细信息
    */
    @PreAuthorize("@perm.hasPermission('app:version$query')")
    @GetMapping(value = "/{id}")
    public R<SysAppVersion> get(@PathVariable("id") Long id) {
        return R.success(sysAppVersionService.getById(id));
    }

    /**
    * 新增
    */
    @PreAuthorize("@perm.hasPermission('app:version$add')")
    @Log(title = "应用版本管理", type = Operation.INSERT)
    @PostMapping
    public R add(@Validated({Add.class}) @RequestBody SysAppVersion model) {
        return R.result(sysAppVersionService.save(model));
    }

    /**
    * 修改
    */
    @PreAuthorize("@perm.hasPermission('app:version$edit')")
    @Log(title = "应用版本管理", type = Operation.UPDATE)
    @PutMapping
    public R edit(@Validated({Update.class}) @RequestBody SysAppVersion model) {
        return R.result(sysAppVersionService.updateById(model));
    }

    /**
    * 删除
    */
    @PreAuthorize("@perm.hasPermission('app:version$remove')")
    @Log(title = "应用版本管理", type = Operation.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable("ids") Long[] ids) {
        return R.result(sysAppVersionService.removeBatchByIds(List.of(ids)));
    }

}